package com.bclz.java;

import java.util.HashSet;
import java.util.Set;

public class Node<A> {

    private Node next;
    private A v;

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public A getV() {
        return v;
    }

    public void setV(A v) {
        this.v = v;
    }

    public boolean hasCycle(){

        if(this.next==null) return false;
        Set<Node> nodes=new HashSet<>();
        Node next=this.next;
        while(!nodes.contains(next)){
            if(next==null){
                return false;
            }
            nodes.add(next);
            next=next.next;
        }
        return true;


    }

    public static void main(String[] args) {

        Node node1=new Node();
        Node node2=new Node();
        Node node3=new Node();
        Node node4=new Node();
        node4.setV(4);
        node3.setNext(node2);
        node3.setV(3);
        node2.setNext(node3);
        node2.setV(2);
        node1.setNext(node2);
        node1.setV(1);
        node1.hashCode();
        System.out.println(node1.hasCycle());

    }

}
